<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'header.php' ?>
<style type="text/css">

</style>
<div class="page-header">
	<h1 style="font-size: 20px;">	
		<a class="a-back" href="<?= admin_url('contents/exam') ?>">
            <i class="fa fa-reply"></i>
		</a>
		考试数据管理
		<!-- 同步学校不可添加数据 -->
		<?php if(! $rmxSchool['school_from']): ?>
			<a href="#modal-exam" class="btn btn-info a-custom" data-toggle="modal">
				<i class="ace-icon fa fa-plus"></i> 添加批次
			</a>
		<?php endif ?>

	</h1>
</div>

<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>批次</th>
				<th>添加时间</th>
				<th>考试列表</th>
				<!-- 同步学校不可修改或删除数据 -->
				<?php if(! $rmxSchool['school_from']): ?>
					<th>操作</th>
				<?php endif ?>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>
<!-- 批次弹窗 -->
<div id="modal-exam" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					编辑批次
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">标题：</label>
								<div class="col-sm-6">
									<input class="form-control"  type="text" name="title" />
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-exam">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	var table = "";
	$(function(){
		// 考试批次列表
		table = $('#dynamic-table').dt({
			"ajaxUrl": '<?= admin_url('contents/get_exam_list')?>',
			"columns": [	
				{"data": function(ret){
	            	return ret.title;
				}},
				{"data": function(ret){
	            	return ret.createtime;
				}},
				{"data": function(ret){
					var id = ret.id;
					<?php if($rmxSchool['school_from'] == X_DATA_HAOZHUANYE): ?>
						// 同步的，使用x_exam_id
						id = ret.x_exam_id;
					<?php endif ?>	
	            	return '<a href="<?= admin_url('contents/exam_room_data/')?>' + id + '" class="mr10"><i class="fa fa-gears"></i> 查看</a>';
				}},
				// 同步学校不可修改或删除数据
				<?php if(! $rmxSchool['school_from']): ?>
					{"data": function(ret){
						var action = '';
						action += '<a data-id="'+ ret.id + '" data-title="'+ ret.title + '" href="#modal-exam"" data-toggle="modal" class="mr10 text-success edit-title"><i class="fa fa-edit"></i> 编辑</a>&nbsp;&nbsp;';
						action += '<a data-id="'+ ret.id + '" data-title="'+ ret.title + '" class="delete-exam text-danger" href="javascript:;"><i class="fa fa-times"></i> 删除</a>';
						return action;
					}}
				<?php endif ?>
			]
		});
		// 修改批次标题时,传递旧值
		$("body").on("click", ".edit-title", function(){
			var id = $(this).data("id");
			var title = $(this).data("title");
			$("[name=id]").val(id);
			$("[name=title]").val(title);
		});
		// 保存批次
		$("#save-exam").click(function(){
			var submitUrl = "<?=admin_url('contents/save_exam_action')?>";
			// 批次id
			var id = $('[name=id]').val();
			// 批次标题
			var title = $('[name=title]').val();
			ajax_post(submitUrl, { id: id, title: title }, function(){
				// 刷新页面
				location.reload();
			});
		});
		// 删除某批次
		$("body").on("click", ".delete-exam", function(){
			var id = $(this).data("id");
			var title = $(this).data("title");
			layer.confirm('确定删除:' + title + '？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('contents/delete_exam_action')?>";
				var postData = { id: id };
				ajax_post(postUrl, postData, function(res){
					table.draw(false);
				})
			});
		})
	})
</script>